package com.tanhua.manage.service;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.manage.mapper.ActivationMapper;
import com.tanhua.manage.mapper.AnalysisByDayMapper;
import com.tanhua.manage.mapper.DayActiveMapper;
import com.tanhua.manage.pojo.AnalysisByDay;
import com.tanhua.manage.pojo.DayActive;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 杨一帆
 * @date 2020/12/14  21:23
 */
@Service
public class DayActiveService extends ServiceImpl<DayActiveMapper, DayActive> {


    @Autowired
    private AnalysisByDayMapper analysis;
    @Autowired
    private ActivationMapper activationMapper;
    @Autowired
    private DayActiveMapper dayActiveMapper;

    /**
     * 查询单表,必须限定性别
     * @param page
     * @param pageSize
     * @param evenOrOdd
     * @return
     */
    public IPage<DayActive> findByPage(Integer page, Integer pageSize, String startdate, String enddate,Integer evenOrOdd) {
        Page<DayActive> infoPage = new Page<>(page, pageSize);
        //设置排序
        infoPage.setDesc("query_date");
        LambdaQueryWrapper<DayActive> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        QueryWrapper queryWrapper = new QueryWrapper();

        if (startdate != null && enddate != null) {
            lambdaQueryWrapper.between(DayActive::getQueryDate,startdate,enddate);
        }

        if (evenOrOdd != null) {
            lambdaQueryWrapper.eq(DayActive::getOddEven, evenOrOdd);
        }
        return super.getBaseMapper().selectPage(infoPage, lambdaQueryWrapper);
    }


    /**
     * 数据存储
     * @param dayActive
     */
    public void saveDayActive(DayActive dayActive) {
        QueryWrapper<AnalysisByDay> query = new QueryWrapper<>();
        List<AnalysisByDay> analysisByDays = analysis.selectList(query);
        for (AnalysisByDay analysisByDay : analysisByDays) {
            dayActive.setRegisterNum(analysisByDay.getNumRegistered()); //注册人数
            dayActive.setLoginNum(analysisByDay.getNumLogin());    //登录人数
            dayActive.setActiveNum(analysisByDay.getNumActive());
            Date date = analysisByDay.getRecordDate();
            String format = DateUtil.format(date, "yyyy-MM-dd");
            dayActive.setQueryDate(format); //日期

            /*//判断dayActive的性别,根据性别查询
            if (dayActive.getSex() == 1) {
                int i = activationMapper.queryActivation(date, 1);
                dayActive.setActNum(i);
                if (i % 2 == 0) {
                    dayActive.setOddEven(0);
                } else {
                    dayActive.setOddEven(1);
                }
            }
            if (dayActive.getSex() == 2) {
                int i = activationMapper.queryActivation(date, 2);
                dayActive.setActNum(i);
                if (i % 2 == 0) {
                    dayActive.setOddEven(0);
                } else {
                    dayActive.setOddEven(1);
                }
            }*/

            int i= activationMapper.queryActivation(format);

            dayActive.setActNum(i);
            if (i % 2 == 0) {
                dayActive.setOddEven(0);
            } else {
                dayActive.setOddEven(1);
            }
            this.dayActiveMapper.insert(dayActive);


        }


    }
}
